import md5 from "../lib/md5.js";
var util = {};


util.scrollData = function (id) {
	let scrollTop = document.querySelector(id).scrollTop;
	let scrollHeight = document.querySelector(id).scrollHeight;
	let clientHeight = document.querySelector(id).clientHeight;
	let range = scrollHeight - scrollTop;
	// console.log(scrollTop,scrollHeight,clientHeight,range);
	if (clientHeight >= range) {
		return true;
	}
	return false;
}

/**按钮位置排序***/
util.sort = function (arr) {
	arr.sort(function (a, b) {
		var x = Number(a.sort);
		var y = Number(b.sort);
		if (x < y) {
			return 1;
		}
		if (x > y) {
			return -1;
		}
		return 0;
	});
}

util.sorts_fun = function (arr, ch) {
	return function (a, b) {
		var v1 = a[arr];
		var v2 = b[arr];
		if (ch)
			return v2 - v1;
		else
			return v1 - v2;
	};
};


/**获取对象属性值***/
util.getObjValue = function (data) {
	let obj = {};
	for (let k in data) {
		obj[k] = data[k];
	}
	return obj;
}

util.uuid = function () {
	let s = [];
	let hexDigits = "0123456789abcdef";
	for (let i = 0; i < 36; i++) {
		s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
	s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
	s[8] = s[13] = s[18] = s[23] = "-";

	let uuid = s.join("");
	return uuid;
}

util.getJpgFileName = function () {
	let fileName = md5.hex_md5(this.uuid()) + ".jpg";
	return fileName;
}

/****去重****/
util.removeDuplicate = function (arr, key) {
		let deWeightThree = () => {
			let map = new Map();
			for (let item of arr) {
				if (!map.has(item[key])) {
					map.set(item[key], item);
				}
			}
			return [...map.values()];
		}
		let tempArr = [];
		if (arr && arr.length > 0)
			tempArr = deWeightThree();
		return tempArr;
	},

	/****去小数0，取整***/
	util.getInt = function getInt(a) {
		let temp = String(a).split(".");
		if (temp[1] == '0' || temp[1] == "00" || temp[1] == '000')
			a = temp[0];
		return Number(a);
	}

/***小数位**/
util.getDecimalPlace = function (p) {
	let num = 10;
	if (p == 2) {
		num = 100;
	} else if (p == 3) {
		num = 1000;
	}
	return num;
}

/****小数计算****/
util.decimalPlaceCount = function (a, p) {
	a = a.toFixed(p);
	a = this.getInt(a);
	return a;
}

util.random6Str = function () {
	const lettersStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	const LEN = 6;
	let randStr = "";
	for (let i = 0; i < LEN; i++) {
		randStr = randStr + lettersStr[Math.floor((Math.random() * lettersStr.length))];
	}
	return randStr;
}

//删除左右两端的空格
util.trim = function (str) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

// 获取当前时间
util.getTime = function (type = "yyyy-MM-dd HH:mm:ss") {
	Date.prototype.Format = function (fmt) {
		var o = {
			"M+": this.getMonth() + 1, //月份
			"d+": this.getDate(), //日
			"H+": this.getHours(), //小时
			"m+": this.getMinutes(), //分
			"s+": this.getSeconds(), //秒
			"q+": Math.floor((this.getMonth() + 3) / 3), //季度
			S: this.getMilliseconds() //毫秒
		};
		if (/(y+)/.test(fmt))
			fmt = fmt.replace(
				RegExp.$1,
				(this.getFullYear() + "").substr(4 - RegExp.$1.length)
			);
		for (var k in o)
			if (new RegExp("(" + k + ")").test(fmt))
				fmt = fmt.replace(
					RegExp.$1,
					RegExp.$1.length == 1 ?
					o[k] :
					("00" + o[k]).substr(("" + o[k]).length)
				);
		return fmt;
	};

	var a = new Date().Format(type);

	return (this.nowtime = a);
}

//转换成分钟
util.changeMinTime = function (str) {
	let arr = String(str).split(".");
	let t = Number(arr[0]) * 60 + Number(arr[1]);
	return t;
}

//计算时间长度
util.caluTLong = function (t1, t2) {
	let tt = this.changeMinTime(t1);
	let ft = this.changeMinTime(t2);
	let tl = tt - ft;
	return tl;
}

util.getWeekDay = function (value) {
	if (String(value).indexOf("Mon") != -1) return "周一";
	if (String(value).indexOf("Tue") != -1) return "周二";
	if (String(value).indexOf("Wed") != -1) return "周三";
	if (String(value).indexOf("Thu") != -1) return "周四";
	if (String(value).indexOf("Fri") != -1) return "周五";
	if (String(value).indexOf("Sat") != -1) return "周六";
	if (String(value).indexOf("Sun") != -1) return "周日";
}

// 时间戳转换为日期
util.add0 = function (m) {
	return m < 10 ? '0' + m : m
}
util.format = function (shijianchuo) {
	//shijianchuo是整数，否则要parseInt转换
	var time = new Date(shijianchuo);
	var y = time.getFullYear();
	var m = time.getMonth() + 1;
	var d = time.getDate();
	var h = time.getHours();
	var mm = time.getMinutes();
	var s = time.getSeconds();
	return y + '-' + this.add0(m) + '-' + this.add0(d) + ' ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s);
}
// 计算俩个日期相差的天数
util.getDaysBetween = function (startDate, enDate) {
	const sDate = Date.parse(startDate)
	const eDate = Date.parse(enDate)
	if (sDate > eDate) {
		return 0
	}
	if (sDate === eDate) {
		return 1
	}
	const days = (eDate - sDate) / (1 * 24 * 60 * 60 * 1000)
	return days
}

export default util;